package com.polaris.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.common.utils.RespBean;
import com.polaris.server.dto.PassengerAgeStruct;
import com.polaris.server.dto.PassengerQueryParam;
import com.polaris.server.pojo.TrafficPassenger;
import com.polaris.server.service.ITrafficPassengerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author polaris
 * @since 2021-06-02
 */
@Api(tags = "乘客数据管理")
@RestController
@RequestMapping("/api/traffic/passenger")
public class TrafficPassengerController {

    private final ITrafficPassengerService passengerService;

    public TrafficPassengerController(ITrafficPassengerService passengerService) {
        this.passengerService = passengerService;
    }
    @ApiOperation(value = "根据条件获取乘客list")
    @GetMapping("/list")
    public RespBean list(@RequestParam Integer page, @RequestParam Integer limit, @RequestBody PassengerQueryParam queryParam){
        try{
            Page<TrafficPassenger> selectPage = new Page<>(page, limit);
            IPage<TrafficPassenger> list = passengerService.list(queryParam, selectPage);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("total", list.getTotal());
            returnMap.put("data", list.getRecords());
            return RespBean.success("获取乘客list成功", returnMap);
        }
        catch (Exception e){
            return RespBean.error("获取乘客list失败,"+e.getMessage());
        }
    }

    @ApiOperation(value = "根据性别获取乘客list")
    @GetMapping("/list/gender")
    public RespBean listByGender(@RequestParam Integer gender, @RequestParam Integer page, @RequestParam Integer limit){
        try{
            Page<TrafficPassenger> selectPage = new Page<>(page, limit);
            IPage<TrafficPassenger> list = passengerService.listByGender(gender, selectPage);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("total", list.getTotal());
            returnMap.put("data", list.getRecords());
            return RespBean.success("获取乘客list成功", returnMap);
        }
        catch (Exception e){
            return RespBean.error("获取乘客list失败,"+e.getMessage());
        }
    }
    @ApiOperation(value = "根据区域获取乘客list")
    @GetMapping("/list/dist")
    public RespBean listByDist(@RequestParam Integer dist, @RequestParam Integer page, @RequestParam Integer limit){
        try{
            Page<TrafficPassenger> selectPage = new Page<>(page, limit);
            IPage<TrafficPassenger> list = passengerService.listByDist(dist, selectPage);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("total", list.getTotal());
            returnMap.put("data", list.getRecords());
            return RespBean.success("获取乘客list成功", returnMap);
        }
        catch (Exception e){
            return RespBean.error("获取乘客list失败,"+e.getMessage());
        }
    }

    @ApiOperation(value = "查询乘客数")
    @GetMapping("/count")
    public RespBean countNormal(){
        return RespBean.success("获取乘客总数成功",passengerService.countNormal());
    }
    @ApiOperation(value = "通过性别查询乘客数")
    @GetMapping("/count/gender")
    public RespBean countByGender(@RequestParam Integer gender){
        return RespBean.success("查询成功", passengerService.countByGender(gender));
    }
    @GetMapping("/gender/count")
    public RespBean countByGender(){
        return RespBean.success("查询成功", passengerService.countByGender());
    }
    @ApiOperation(value = "通过区域查询乘客数")
    @GetMapping("/count/dist")
    public RespBean countByDist(@RequestParam Integer dist){
        return RespBean.success("查询成功", passengerService.countByDist(dist));
    }

    @ApiOperation(value = "添加乘客")
    @PostMapping("/add")
    public RespBean createPassenger(@RequestBody @Validated PassengerQueryParam queryParam){
        if(StringUtils.hasText(queryParam.toString())){
            TrafficPassenger passenger = passengerService.createPassenger(queryParam);
            if(passenger==null){
                return RespBean.error("添加用户失败");
            }
            return RespBean.success("添加乘客成功", passenger);
        }
        return RespBean.error("信息不能为空");
    }
    @ApiOperation(value = "更新乘客信息")
    @PostMapping("/update")
    public RespBean updatePassenger(@RequestBody @Validated PassengerQueryParam queryParam){
        TrafficPassenger updatePassenger = passengerService.updatePassenger(queryParam);
        if(updatePassenger==null){
            return RespBean.error("更新乘客信息失败");
        }
        return RespBean.success("更新乘客信息成功",updatePassenger);
    }
    @ApiOperation(value = "id查询乘客")
    @GetMapping("/get")
    public RespBean getPassengerById(@RequestParam Long id){
        TrafficPassenger resultPassenger;
        resultPassenger = passengerService.getById(id);
        if (resultPassenger==null){
            return RespBean.error("查询乘客结果为空");
        }
        return RespBean.success("乘客信息查询成功", resultPassenger);
    }
    @ApiOperation(value = "passenger_id查询乘客")
    @GetMapping("/get/pid")
    public RespBean getPassengerByPid(@RequestParam String pid){
        TrafficPassenger resultPassenger;
            resultPassenger=passengerService.getByPassengerId(pid);
        if (resultPassenger==null){
            return RespBean.error("查询乘客结果为空");
        }
        return RespBean.success("乘客信息查询成功", resultPassenger);
    }
    @ApiOperation(value = "id删除乘客")
    @GetMapping("/delete")
    public RespBean deleteById(@RequestParam Long id){
        Boolean delete = passengerService.deleteById(id);
        if(delete){
            return RespBean.success("删除乘客成功");
        }
        return RespBean.success("删除乘客失败");
    }
    @ApiOperation(value = "pid删除乘客")
    @GetMapping("/delete/pid")
    public RespBean deleteByPid(@RequestParam String pid){
        Boolean delete = passengerService.deleteByPassengerId(pid);
        if(delete){
            return RespBean.success("删除乘客成功");
        }
        return RespBean.error("删除乘客失败");
    }
    @ApiOperation(value = "获取乘客年龄结构")
    @GetMapping("/struct")
    public RespBean passengerAgeStruct(){
        List<PassengerAgeStruct> ageStructs = passengerService.passengerAgeStruct();
        if(ageStructs==null){
            return RespBean.error("获取乘客年龄结构失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", ageStructs);
        return RespBean.success("获取乘客年龄结构成功", map);
    }
}
